package org.ranshiyan.controller;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.ranshiyan.pojo.Teacher;
import org.ranshiyan.service.TeacherService;
import org.ranshiyan.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    @GetMapping
    public Object get(){
        List<Teacher> list = teacherService.list();
        return  "111"+list.toString();
    }

    @RequestMapping("/fileupload")
    public String fileupload(@RequestParam("file")MultipartFile file, HttpServletRequest request){
        //获取文件名
        String filename = file.getOriginalFilename();
        //设置文件上传的保存路径
        String dirpath = "D:\\file\\新建文件夹\\";
        //如果保存文件的地址不存在，就先创建目录
        File filePath = new File(dirpath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //重新命名文件
        String newFilename = UUID.randomUUID().toString().substring(0,5)+filename;
        System.out.println(dirpath);
        try {
            //使用multipartFile接口的方法完成文件上传到指定位置
            file.transferTo(new File(dirpath+newFilename));
            System.out.println(newFilename);
            /*String[] split = newFilename.split("\\.");*/
            ExcelUtil.OutExcel(dirpath+newFilename);
        } catch (Exception e) {

        }
        return null;
    }
}
